/*****************************************************************
 **                                                             	**
 **  Copyright (C) 2004 Amlogic,Inc.                            	**
 **  All rights reserved                                        	**
 **        Filename : Debug.c /Project:AVOS		       **
 **        Revision : 1.0                                       	**
 **                                                             	**
 *****************************************************************/
#include "../menu.h"
#include "DebugEvent.h"

void SetWinDebugPalette(void)
{
  YUVENTRY mask_para[4] = 
  {
    0, 79, 4095, 4095
  };
  AWSetAlphaMask(0, 4, mask_para);
  YUVENTRY palette_para[16] = 
  {
    0x8000 | 16392, 0x8000 | 21176, 0x8000 | 17752, 0x8000 | 16392, 17592,
      19496, 19832, 19656, 19992, 18104, 17673, 17817, 15864, 15721, 15912,
      16344
  };
  AWSetPalette(0, 16, palette_para);
  return ;
}

void SetWinDebugPos(HWND *pControl, INT8U CurOsdSize)
{
  switch (CurOsdSize)
  {

    case OsdSize576i:
    case OsdSize576p:
    case OsdSize480i:
    case OsdSize480p:
    case OsdSize480iCVBS:
    case OsdSize576iCVBS:
      #ifdef SUPPORT_VGA
      case OsdSizeVga:
      #endif 
      RECT StandardWindowRect = 
      {
        0, 0, 720, 576
      }
      ;
      pControl->winrect = StandardWindowRect;
      CREATEWINDOWPARAM StandardWindowPara = 
      {
        1, 
        {
          {
            0, 0, 720, 576
          }
          , 
          {
            0, 0, 0, 0
          }
          , 
          {
            0, 0, 0, 0
          }
          , 
          {
            0, 0, 0, 0
          }
        }
        , 
        {
          PF_PALETTE_4, 0, 0, 0
        }
        , SetWinDebugPalette
      }
      ;
      AWSendMessage(pControl, WM_WINDOW_RESET_DRAW_AREA, (INT32S)
        &StandardWindowPara);
      break;
  } //end of switch
  return ;
}

HWND *CreateControlWinDebug(HWND *pParent)
{

  /*   switch(CurOsdSize){
  case OsdSize576i:
  case OsdSize576p:
  case OsdSize480i:
  case OsdSize480p:
  case OsdSize480iCVBS:
  case OsdSize576iCVBS:
  #ifdef SUPPORT_VGA
  case OsdSizeVga:
  #endif
   */
  HWND *pNewControl;
  CREATEWINDOWPARAM window_para = 
  {
    1, 
    {
      {
        0, 0, 720, 576
      }
      , 
      {
        0, 0, 0, 0
      }
      , 
      {
        0, 0, 0, 0
      }
      , 
      {
        0, 0, 0, 0
      }
    }
    , 
    {
      PF_PALETTE_4, 0, 0, 0
    }
    , SetWinDebugPalette
  };
  //Create Menu Window -- 1
  pNewControl = AWCreateWinControl("TWindow", "WinDebug", WS_OVERLAPPEDWINDOW,
    0, 0, 720, 576, NULL, (INT32S) &window_para, NULL);
  pNewControl->win_id = 0;
  //	break;
  //   	}
  return pNewControl;
}

void SetDebugConsolePos(HWND *pControl, INT8U CurOsdSize)
{
  int iLoop;
  switch (CurOsdSize)
  {

    case OsdSize480i:
    case OsdSize480p:
    case OsdSize480iCVBS:
      #ifdef SUPPORT_VGA
      case OsdSizeVga:
      #endif 

      pControl->winrect.top = pControl->parent->winrect.top + 20;
      pControl->winrect.left = pControl->parent->winrect.left + 30;
      pControl->winrect.width = 720;
      pControl->winrect.height = 480;
      pControl->draw_info_num = 14;
      RECT StandardDrawRect[14] = 
      {
        {
          pControl->winrect.left + 0, pControl->winrect.top + 0, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 30, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 60, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 90, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 120, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 150, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 180, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 210, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 240, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 270, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 300, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 330, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 360, 680, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 390, 680, 30
        }
        , 
      }
      ;
      for (iLoop = 0; iLoop < 14; iLoop++)
        memcpy(&(pControl->draw_info[iLoop]), &StandardDrawRect[iLoop], sizeof
          (RECT));
      break;
    case OsdSize576i:
    case OsdSize576p:
    case OsdSize576iCVBS:
      pControl->winrect.top = pControl->parent->winrect.top + 30;
      pControl->winrect.left = pControl->parent->winrect.left + 50;
      pControl->winrect.width = 680;
      pControl->winrect.height = 576;
      pControl->draw_info_num = 14;
      RECT DrawRect576[14] = 
      {
        {
          pControl->winrect.left + 0, pControl->winrect.top + 0, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 36, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 72, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 108, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 144, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 180, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 216, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 252, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 288, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 324, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 360, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 396, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 432, 670, 30
        }
        , 
        {
          pControl->winrect.left + 0, pControl->winrect.top + 468, 670, 30
        }
        , 
      }
      ;
      for (iLoop = 0; iLoop < 14; iLoop++)
        memcpy(&(pControl->draw_info[iLoop]), &DrawRect576[iLoop], sizeof(RECT))
          ;
      break;
  } //end of switch

  return ;
}

INT32S DebugConsoleMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
  switch (msg_type)
  {
    case WM_SHOWWINDOW:
      return AWDebugOnShow(hwnd, lpParam);
    case WM_HIDEWINDOW:
      return AWDebugOnHide(hwnd, lpParam);
    case WM_PAINT:
      return AWDebugOnPaint(hwnd, lpParam);
  }
  return 0;
}

HWND *CreateControlDebugConsole(HWND *pParent)
{
  //Create Control -- DebugConsole
  HWND *pNewControl = AWCreateWinControl("TControl", "DebugConsole", 
    (WS_CHILDWINDOW | WS_CHILDWINDOW), 0, 0, 720, 576, pParent, 0,
    DebugConsoleMsgProc);
  pNewControl->win_id = 1;
  pNewControl->draw_info_num = 114;
  pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO)
    *pNewControl->draw_info_num);
  DRAWINFO DrawDatas[14] = 
  {    
  	{0, 0, 0, 0, (INT32U)debug_string[0], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[1], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[2], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[3], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[4], DT_STRING_DATA},
	
	{0, 0, 0, 0, (INT32U)debug_string[5], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[6], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[7], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[8], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[9], DT_STRING_DATA},
	
	{0, 0, 0, 0, (INT32U)debug_string[10], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[11], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[12], DT_STRING_DATA},
	{0, 0, 0, 0, (INT32U)debug_string[13], DT_STRING_DATA},
  };
  memcpy(pNewControl->draw_info, DrawDatas, sizeof(DRAWINFO) *pNewControl->draw_info_num);
  return pNewControl;
}

void SetDebugPagePos(HWND *pControl, INT8U CurOsdSize)
{
  int iLoop;
  switch (CurOsdSize)
  {

    case OsdSize480i:
    case OsdSize480p:
    case OsdSize480iCVBS:
      #ifdef SUPPORT_VGA
      case OsdSizeVga:
      #endif 
      pControl->winrect.top = pControl->parent->winrect.top + 440;
      pControl->winrect.left = pControl->parent->winrect.left + 250;
      pControl->winrect.width = 220;
      pControl->winrect.height = 40;
      pControl->draw_info_num = 1;
      RECT StandardDrawRect[1] = 
      {
        {pControl->winrect.left + 0, pControl->winrect.top + 0, 200, 30}, 
      };
      for (iLoop = 0; iLoop < 1; iLoop++)
        memcpy(&(pControl->draw_info[iLoop]), &StandardDrawRect[iLoop], sizeof(RECT));          
      break;
    case OsdSize576i:
    case OsdSize576p:
    case OsdSize576iCVBS:
      pControl->winrect.top = pControl->parent->winrect.top + 534;
      pControl->winrect.left = pControl->parent->winrect.left + 250;
      pControl->winrect.width = 220;
      pControl->winrect.height = 40;
      pControl->draw_info_num = 1;
      RECT DrawRect576[1] = 
      {
        {pControl->winrect.left + 0, pControl->winrect.top + 0, 200, 30},     
      };    
      for (iLoop = 0; iLoop < 1; iLoop++)
        memcpy(&(pControl->draw_info[iLoop]), &DrawRect576[iLoop], sizeof(RECT));          
      break;
  } //end of switch
  return ;
}

INT32S DebugPageMsgProc(PHWND hwnd, INT32U msg_type, INT32S lpParam)
{
  switch (msg_type)
  {
    case WM_PAINT:
      return DebugPageOnPaint(hwnd, lpParam);
    case WM_KEYDOWN:
      return DebugPageOnKeyPress(hwnd, lpParam);
  }
  return 0;
}

HWND *CreateControlDebugPage(HWND *pParent)
{
  //Create Control -- DebugPage
  HWND *pNewControl = AWCreateWinControl("TControl", "DebugPage", 
    (WS_CHILDWINDOW | WS_CHILDWINDOW), 250, 440, 220, 40, pParent, 0,DebugPageMsgProc);   
  pNewControl->win_id = 2;
  pNewControl->draw_info_num = 1;
  pNewControl->draw_info = (PDRAWINFO)AVMem_umalloc(sizeof(DRAWINFO)
    *pNewControl->draw_info_num);
  DRAWINFO DrawDatas[1] = 
  {
    {0, 0, 0, 0, (INT32U)debug_page, DT_STRING_DATA}
   };   
 memcpy(pNewControl->draw_info, DrawDatas, sizeof(DRAWINFO) *pNewControl->draw_info_num);
   return pNewControl;
}

HWND *CreateMenuWinDebug(void)
{
  HWND *pNewControl;
  TWINDOWS *pWin;
  HWND *pWinDebug = CreateControlWinDebug(NULL);
  SetWinDebugPos(pWinDebug, CurrentOsdSize);

  pNewControl = CreateControlDebugConsole(pWinDebug);
  SetDebugConsolePos(pNewControl, CurrentOsdSize);

  pNewControl = CreateControlDebugPage(pWinDebug);
  SetDebugPagePos(pNewControl, CurrentOsdSize);

  pWin = (TWINDOWS*)pWinDebug;
  pWin->pFocusControl = AWFindWinCtrlByName("DebugPage");
  return pWinDebug;
}

void WinResetWinDebugControlsPos(HWND *pControl, INT8U CurOsdSize)
{
  switch (pControl->win_id)
  {
    case 0:
      //WinDebug
      SetWinDebugPos(pControl, CurOsdSize);
      break;
    case 1:
      //DebugConsole
      SetDebugConsolePos(pControl, CurOsdSize);
      break;
    case 2:
      //DebugPage
      SetDebugPagePos(pControl, CurOsdSize);
      break;
  } //end of switch
}

void WinResetWinDebugPos(HWND *pControl, INT8U CurOsdSize)
{
  HWND *pChildControl;
  if (pControl == NULL)
    pControl = AWFindWinCtrlByName("WinDebug");
  if (pControl == NULL)
    return ;

  WinResetWinDebugControlsPos(pControl, CurOsdSize);
  pChildControl = pControl->children;
  while (pChildControl != NULL)
  {
    WinResetWinDebugPos(pChildControl, CurOsdSize);
    pChildControl = pChildControl->siblings;
  }
  return ;
}
